package travelDream.beans;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import traveldream.ejb.DTO.HotelDTO;
import traveldream.ejb.interfaces.*;

@ManagedBean(name = "HotelEditManaged")
@ViewScoped
public class HotelEditManaged {

	private HotelDTO hDTO;
	private boolean notPrima = false;
	@EJB
	HotelEditBeanLocal HotelEditBeanLocal;
	
	public HotelEditManaged(){
	}
	
	public void loadHotel(){
		if(notPrima == false){
			FacesContext facesContext = FacesContext.getCurrentInstance();
			int passedParameter = Integer.parseInt((facesContext.getExternalContext().getRequestParameterMap().get("id")));
			try{
			hDTO = HotelEditBeanLocal.find(passedParameter);
			}
			catch(NotFindException e){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Id wrong!! Please, go back and try again!!"));
			}
			notPrima = true;
		}
	}
	public String update(){
		HotelEditBeanLocal.update(hDTO);
		return "hotel.xhtml?faces-redirect=true";
	}

	public HotelDTO gethDTO() {
		return hDTO;
	}

	public void sethDTO(HotelDTO hDTO) {
		this.hDTO = hDTO;
	}

	public HotelEditBeanLocal getHotelEditBeanLocal() {
		return HotelEditBeanLocal;
	}

	public void setHotelEditBeanLocal(HotelEditBeanLocal hotelEditBeanLocal) {
		HotelEditBeanLocal = hotelEditBeanLocal;
	}
	
}
